package com.imooc.girl.service;

import com.imooc.girl.domain.Girl;
import com.imooc.girl.enums.ResultEnum;
import com.imooc.girl.exception.GirlException;
import com.imooc.girl.respository.GirlRepostory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * Created by 10077 on 2018/3/7.
 * desc:
 */

@Service
public class GirlService {

    @Autowired
    private GirlRepostory girlRepostory;

    /**
     * 事物控制只能插入一条数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertTwo(){
        Girl girlA = new Girl();
        girlA.setAge(18);
        girlA.setCupSize("A");
        girlRepostory.save(girlA);

        Girl girlB = new Girl();
        girlB.setAge(19);
        girlB.setCupSize("B");
        girlRepostory.save(girlB);
    }

    public void getAge(Integer id){
            Girl girl = girlRepostory.getOne(id);
            Integer age = girl.getAge();
            if (age < 10) {
                //返回"你还在上小学吧" code=100
                throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
            }else if (age > 10 && age < 16) {
                //返回"你可能在上初中" code=101
                throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
            }

            //如果>16岁,加钱
            //...
        }

        /**
         * 通过Id查询一个女生的信息
         * @param id
         * @return
         */
        public Girl findOne(Integer id) {
            return girlRepostory.getOne(id);
        }
}

